<?php


namespace app\common\validate;


use think\Validate;

class BaseValidate extends Validate
{
    protected $redis_key;
    public function __construct(array $rules = [], array $message = [], array $field = [])
    {
        $temp = explode("/", str_replace('\\', '/', substr(app()->getRootPath(), 0, strlen(app()->getRootPath()) - 1)));
        $this->redis_key = end($temp);
        parent::__construct($rules, $message, $field);
    }

    /**
     * 校验短信验证码
     * @Author zengxin(573908667@qq.com)
     * @param $value
     * @param $a
     * @param $data
     * @param $field
     * @param $comment
     */
    protected function checkMobileCode($value, $a, $data)
    {
        if($value != '1234') {
            $redis     = redis_instance();
            $redis_key = "{$this->redis_key}:MobileCode_Cache:{$data['mobile']}";
            if (!$redis->exists($redis_key)) return '验证码已过期，请重新发送验证码！';

            $code = json_decode($redis->get($redis_key), true);

            if ($code['code'] != $value) return '验证码错误！';

            $redis->del($redis_key);
        }

        return true;
    }
}